package cn.yy.cloudpay.application;

import com.newland.me.ConnUtils;
import com.newland.me.DeviceManager;
import com.newland.mtype.ConnectionCloseEvent;
import com.newland.mtype.Device;
import com.newland.mtype.Module;
import com.newland.mtype.ModuleType;
import com.newland.mtype.event.DeviceEventListener;
import com.newland.mtype.module.common.printer.Printer;
import com.newland.mtype.module.common.scanner.BarcodeScanner;
import com.newland.mtype.module.common.scanner.BarcodeScannerManager;
import com.newland.mtypex.nseries.NSConnV100ConnParams;
import com.newland.mtypex.nseries3.NS3ConnParams;

import cn.yy.cloudpay.BuildConfig;
import cn.yy.cloudpay.R;
import cn.yy.cloudpay.cardread.N900Device;
import cn.yy.cloudpay.util.ConfigureLog4J;
import cn.yy.cloudpay.util.CrashHandler;
import cn.yy.cloudpay.util.StringUtil;
import cn.yy.cloudpay.view.ToastUtil;
import cn.yy.pos.launcher.service.Operator;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import org.xutils.x;

public class CloudPayApplication extends Application {

	private Context mContext;
	private Operator operator;
	private DeviceListener mListener;
	//新大陆N900设备管理对象
	private static final String K21_DRIVER_NAME = "com.newland.me.K21Driver";

	//连接方式
	private NSConnV100ConnParams mParams;
	private DeviceManager mDeviceManager = ConnUtils.getDeviceManager();
	
	private Device mDevice;
	
	private BarcodeScanner mScanner;
	private BarcodeScannerManager mScannerManager;
	private Printer mPrinter;
	private String serviceAddress; // 服务器的地址
	public static int from = 0;
	public static String deviceSN="";
	
	@Override
	public void onCreate() {
		super.onCreate();
		ConfigureLog4J.configure(this);
		CrashHandler.getInstance().init(getApplicationContext());
		Log.d("TAG", "CloudPayApplication----onCreate");
		mContext = getApplicationContext();
		try {
			initDeviceManager();
		} catch (Exception e) {
			e.printStackTrace();
			//提醒用户设备初始化失败
		}
		x.Ext.init(this);
		//是否是开发、调试模式
		x.Ext.setDebug(BuildConfig.DEBUG);//是否输出debug日志，开启debug会影响性能
	}

	/**
	 * 初始化设备管理器
	 * @throws Exception 
	 */
	private void initDeviceManager() throws Exception {
//		Log.d("TAG", "初始化设备");
		this.mListener = new DeviceListener();
//		this.mParams = new NSConnV100ConnParams();
//		this.mDeviceManager.init(mContext, getString(R.string.k21_driver_name), mParams, mListener);
		this.mDeviceManager = ConnUtils.getDeviceManager();
		mDeviceManager.init(mContext, K21_DRIVER_NAME, new NS3ConnParams(), mListener);

		mDeviceManager.connect();
		mDevice = mDeviceManager.getDevice();
		mDeviceManager.getDevice().setBundle(new NS3ConnParams());
	}
	
	/**
	 * 返回全局上下文单例
	 * @return
	 */
	public Context getContextInstance() {
		if(mContext == null) {
			synchronized (this) {
				mContext = getApplicationContext();
			}
		}
		return mContext;
	}
	
	/**
	 * 设置操作员
	 * @param operator
	 */
	public void setOperator(Operator operator) {
		this.operator = operator;
	}
	
	/**
	 * 获取操作员对象
	 * @return
	 */
	public Operator getOperator() {
		return operator;
	}
	
	/**
	 * 判断是否登录
	 * @return
	 */
	public boolean isLogin() {
		if(operator != null) {
			return true;
		}
		return false;
	}

	/**
	 * 获取设备SN码
	 */
	public String getDeviceSN() {
		Device d = getDeviceInstance();
		String sn = "";
		if(d != null){
			sn = getDeviceInstance().getDeviceInfo().getSN();
		}
		if(StringUtil.isEmpty(sn)){
			sn = Build.SERIAL;
		}
		return sn;
	}

	public String isMT(){
		String sn = getDeviceSN();
		String key = "";
		if(!StringUtil.isEmpty(sn)){
			key = sn.substring(0,1);
		}
		if("m".equals(key.toLowerCase())){
			return "2";
		}else{
			return "1";
		}
	}

	/**
	 * 获取设备
	 * @return
	 */
	public Device getDeviceInstance() {
		if(mDevice == null) {
			synchronized (this) {
				if(mDevice == null) {
					mDevice = mDeviceManager.getDevice();
				}
			}
		}
		return mDevice;
	}

	/**
	 * 获取设备
	 * @return
	 */
	public DeviceManager getDeviceManager() {
		return mDeviceManager;
	}

	/**
	 * 获取设备 中间件 模块
	 * @param moduleType
	 * @return
	 */
	private Module getModule(ModuleType moduleType) {
		Module module = null;
		try {
			module = getDeviceInstance().getStandardModule(moduleType);
		} catch (Exception e) {
			e.getStackTrace();
		}
		return module;
	}
	
	/**
	 * 获取打印机对象
	 * @return
	 */
	public Printer getPrinter() {
		if(mPrinter == null) {
			mPrinter = (Printer) getModule(ModuleType.COMMON_PRINTER);
		}
		return mPrinter;
	}
	
	/**
	 * 获取扫描管理器
	 * @return
	 */
	private BarcodeScannerManager getScannerManager() {
		if(mScannerManager == null) {
			mScannerManager = (BarcodeScannerManager) getModule(ModuleType.COMMON_BARCODESCANNER);
		}
		return mScannerManager;
	}
	
	/**
	 * 获取扫描器对象
	 * @return
	 */
	public BarcodeScanner getScanner() {
		if(mScanner == null) {
			mScanner = getScannerManager().getDefault();
		}
		return mScanner;
	}
	
	/**
	 * 摧毁设备，回收资源
	 */
	public void destroyDevice() {
		if(mDevice.isAlive()) {
			mDevice.destroy();
		}
		mDeviceManager.disconnect();
		mDeviceManager.destroy();
		mDevice = null;
		mDeviceManager = null;
		mScanner = null;
		mPrinter = null;
		mScannerManager = null;
//		Log.d("TAG", "销毁设备");
	}
	
	/**
	 * 设备连接监听器
	* @ClassName: DeviceListener  
	* @author [shibx]（永易科技） 
	* @date 2016年5月25日 下午3:03:23
	 */
	private class DeviceListener implements DeviceEventListener<ConnectionCloseEvent> {

		@Override
		public Handler getUIHandler() {
			return null;
		}

		@Override
		public void onEvent(ConnectionCloseEvent arg0, Handler arg1) {
//			if(arg0.isSuccess()) {
//				Log.d("TAG", "成功！");
//			}else {
//				Log.d("TAG", "失败！");
//			}
		}
	}

	public String getServiceAddress() {
		return serviceAddress;
	}

	public void setServiceAddress(String serviceAddress) {
		this.serviceAddress = serviceAddress;
	}

	@Override
	public void onTerminate() {
		// TODO Auto-generated method stub
		super.onTerminate();
	}
	
	
}
